package com.ruoyi.user.service.impl;

import java.util.List;

import com.ruoyi.user.domain.vo.UsrRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.user.domain.Role;
import com.ruoyi.user.mapper.UserMapper;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.service.IUserService;

/**
 * 用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class UserServiceImpl implements IUserService 
{
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询用户信息
     * 
     * @param usrId 用户信息主键
     * @return 用户信息
     */
    @Override
    public User selectUserByUerId(Long usrId)
    {
        return userMapper.selectUserByUerId(usrId);
    }

    /**
     * 查询用户信息列表
     * 
     * @param user 用户信息
     * @return 用户信息
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户信息
     * 
     * @param user 用户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertUser(User user)
    {
        int rows = userMapper.insertUser(user);
        insertRole(user);
        return rows;
    }

    /**
     * 修改用户信息
     * 
     * @param user 用户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateUser(User user)
    {
        userMapper.deleteRoleByRoleId(user.getUsrId());
        insertRole(user);
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除用户信息
     * 
     * @param usrIds 需要删除的用户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUserByUerIds(Long[] usrIds)
    {
        userMapper.deleteRoleByRoleIds(usrIds);
        return userMapper.deleteUserByUerIds(usrIds);
    }

    /**
     * 删除用户信息信息
     * 
     * @param usrId 用户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUserByUerId(Long usrId)
    {
        userMapper.deleteRoleByRoleId(usrId);
        return userMapper.deleteUserByUerId(usrId);
    }

    @Override
    public List<Role> selectRoleList(Long roleId) {
        return userMapper.selectRoleList(roleId);
    }

    @Override
    public List<Role> selectRole() {
        return userMapper.selectRole();
    }

    /**
     * 新增角色信息信息
     * 
     * @param user 用户信息对象
     */
    public void insertRole(User user)
    {
        List<Role> roleList = user.getRoleList();
        Long uerId = user.getUsrId();
        if (StringUtils.isNotNull(roleList))
        {
            List<Role> list = new ArrayList<Role>();
            for (Role role : roleList)
            {
                role.setRoleId(uerId);
                list.add(role);
            }
            if (list.size() > 0)
            {
                userMapper.batchRole(list);
            }
        }
    }
}
